//
//  IntentHandler.m
//  HMIntent
//
//  Created by orvibo on 2018/12/26.
//  Copyright © 2018 orvibo. All rights reserved.
//

#import "IntentHandler.h"
#import "HMSceneIntent.h"
#import "HMIntentHeader.h"

@interface IntentHandler () <HMSceneIntentHandling>

@end

@implementation IntentHandler


- (id)handlerForIntent:(INIntent *)intent {
    [self initHomeMate];
    
    
    return self;
}

- (void)handleHMScene:(HMSceneIntent *)intent completion:(void (^)(HMSceneIntentResponse *response))completion{
    
    if ([intent isKindOfClass:[HMSceneIntent class]]) {
        
        [self sceneControlWithIntent:intent completion:^(KReturnValue returnValue, NSDictionary *returnDic) {
            
            // 如果执行失败，则跳转到App，执行成功则直接提示
            HMSceneIntentResponseCode code = HMSceneIntentResponseCodeContinueInApp;
            if (returnValue == KReturnValueSuccess) {
                code = HMSceneIntentResponseCodeSuccess;
            }
            
            // 最开始测试，一直执行异常，是因为没有调用completion方法回调
            HMSceneIntentResponse *response = [[HMSceneIntentResponse alloc]initWithCode:code userActivity:nil];
            response.notLogin = @"未登录";
            response.controlFailure = @"控制失败";
            if (completion) {
                completion(response);
            }
        }];
    }
}

-(void)initHomeMate{
    
    [HMSDK setDebugEnable:YES];
    [HMSDK initWithAppName:@"ZhiJia365"];
    // 登录账号
    //[self loginAccount:nil];
}

-(void)loginAccount:(commonBlock)completion
{
    // 同步所有数据，然后刷新显示
    NSString *userName = @"18620330995";
    NSString *password = md5WithStr(@"123456");
    
    [userAccout() widgetLoginWithUserName:userName password:password completion:^(KReturnValue value) {
        
        NSLog(@"登录%@",(value == KReturnValueSuccess) ? @"成功" : @"失败");
        
        if (value == KReturnValueSuccess) { // 登录成功，保存该账号
            NSLog(@"IntentScene: 登录成功");
        } else if (value == KReturnValueAccountOrPWDERR) {
            NSLog(@"IntentScene: 用户名或密码错误");
            
        } else if(value == KReturnValueFamilyInvalid) {
            NSLog(@"IntentScene: 家庭失效(家庭已不存在)");
            // 家庭失效则删除已保存的情景数据
        }
        if (completion) {
            completion(value);
        }
    }];
}

-(void)sceneControlWithIntent:(HMSceneIntent *)intent completion:(SocketCompletionBlock)completion API_AVAILABLE(ios(12.0)){
    
    commonBlock sceneControl = ^(KReturnValue value){
        
        if (value == KReturnValueSuccess) {
            
            EnableSceneServiceCmd *cmd = [EnableSceneServiceCmd object];
            cmd.userName = userAccout().userName;
            cmd.sceneNo = intent.sceneNo;
            cmd.sendToServer = YES;
            sendCmd(cmd, completion);
            
        }else{
            if (completion) {
                completion(value,nil);
            }
        }
    };
    
    if ([RemoteGateway shareInstance].isSocketConnected) {
        sceneControl(KReturnValueSuccess);
    }else{
        [self loginAccount:sceneControl];
    }
}

@end
